package com.example.androidshixi;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;

import java.util.Date;

public class AddArticleActivity extends AppCompatActivity {
    private EditText titleEditText, contentEditText;
    private Button submitButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_article);

        titleEditText = findViewById(R.id.article_title_edit_text);
        contentEditText = findViewById(R.id.article_content_edit_text);
        submitButton = findViewById(R.id.submit_button);

        submitButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String title = titleEditText.getText().toString().trim();
                String content = contentEditText.getText().toString().trim();

                if (title.isEmpty() || content.isEmpty()) {
                    showAlertDialog("提示", "标题和内容不能为空");
                } else {
                    // 创建新的文章对象
                    Article newArticle = new Article(
                            title,
                            UserSessionManager.getInstance(AddArticleActivity.this).getLoggedInUsername(),
                            new Date(),
                            content
                    );

                    // 直接跳转到我的文章页并传递新文章
                    Intent intent = new Intent(AddArticleActivity.this, MyArticlesActivity.class);
                    intent.putExtra("new_article", newArticle);
                    startActivity(intent);
                    finish();
                }
            }
        });
    }

    private void showAlertDialog(String title, String message) {
        new AlertDialog.Builder(this)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton("确定", null)
                .show();
    }

}
